package com.deruan.hotcore.version;

import java.util.Iterator;
import java.util.List;

import org.columba.addressbook.parser.SyntaxException;
import org.jdom.Document;
import org.jdom.Element;

public class VersionParser 
{
	private Document doc;

	protected Element parentElement;

	private Element root;

	public VersionParser() 
	{
		doc = new Document();
		root = new Element("versions");
		doc.addContent(root);
		parentElement = root;
	}

	public VersionParser(Document document) throws SyntaxException {
		if (document == null)
			throw new IllegalArgumentException("document == null");

		this.doc = document;

		this.root = doc.getRootElement();

		if (!root.getName().equalsIgnoreCase("versions")) {
			// wrong xml-format
			throw new SyntaxException("Root element must be <vcard>!");
		}
	}

	public Element getRootElement() {
		return root;
	}

	public String get(String key) {
		Element child = getRootElement().getChild(key);
		if (child == null) {
			child = new Element(key);
			getRootElement().addContent(child);
		}
		return child.getTextNormalize();
	}

	public Document getDocument() {
		return doc;
	}

	public void addVersionInfo(VersionModel model) 
	{
		Element child = new Element("version");
		child.setAttribute("filename", model.getFileName());
        child.setAttribute("filetype", model.getFileType());
        getRootElement().addContent(child);
        child.setText(model.getVersion().toString());
	}
}
